<template>
  <a-card :bordered="false" class="br-10">
    <div class="table-page-search">
      <a-form>
        <a-form-item label="名称">
          <a-input placeholder="请输入" v-model="queryParam.keyword" />
        </a-form-item>
        <span class="search-submitButtons">
          <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
          <a-button @click="() => (queryParam = { status: '' })">重置</a-button>
        </span>
      </a-form>
    </div>
    <div class="table-operator">
      <a-button type="primary" @click="handleAdd">新增</a-button>
      <a-button type="danger" @click="delByIds(ids)">批量删除</a-button>
    </div>
    <s-table
      size="default"
      ref="table"
      rowKey="id"
      :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      :columns="columns"
      :data="loadData"
    >
      <span slot="serial" slot-scope="text, record, index">
        {{ (pageNum - 1) * pageSize + index + 1 }}
      </span>
      <span slot="action" slot-scope="text, record">
        <a-button type="primary" ghost size="small" class="_mr-5" @click="handleEdit(record)">
          编辑
        </a-button>
        <a-button type="primary" ghost size="small" class="_mr-5" @click="delByIds([record.id])">
          删除
        </a-button>
      </span>
    </s-table>
    <duties-form ref="dutiesForm" @add="$refs.table.refresh(true)" @upd="$refs.table.refresh()" />
  </a-card>
</template>

<script>
import { STable } from '@/components'
import { postList, postRemove } from '@/api/system/system'
import dutiesForm from './modules/dutiesForm'
export default {
  name: 'MemberType',
  components: {
    STable,
    dutiesForm
  },
  data() {
    return {
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },

      // 查询参数
      queryParam: { status: '' },
      // 表头
      columns: [
        {
          title: '序号',
          scopedSlots: { customRender: 'serial' },
          width: '80px'
        },
        {
          title: '职务',
          dataIndex: 'name',
          width: '200px',
          ellipsis: true
        },
        {
          title: '备注',
          dataIndex: 'remark',
          width: '200px',
          ellipsis: true
        },
        {
          title: '操作',
          width: '150px',
          fixed: 'right',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
        }
      ],
      selectedRowKeys: [],
      selectedRows: [],
      ids: [],
      pageNum: 1,
      pageSize: 10,
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        var params = {
          current: parameter.pageNum,
          size: parameter.pageSize
        }
        return postList(Object.assign(params, this.queryParam)).then(res => {
          this.pageNum = parameter.pageNum
          this.pageSize = parameter.pageSize
          this.selectedRowKeys = []
          this.selectedRows = []
          this.ids = []
          return res
        })
      }
    }
  },
  created() {},
  methods: {
    handleAdd() {
      var params = {
        title: '新增职务',
        type: 'add'
      }
      this.$refs.dutiesForm.add(params)
    },
    handleEdit(record) {
      var params = {
        title: '编辑职务',
        type: 'edit',
        data: record
      }
      this.$refs.dutiesForm.edit(params)
    },

    handleOk() {
      this.$refs.table.refresh(true)
    },
    handleReload() {
      this.$refs.table.refresh()
    },
    delByIds(ids) {
      if (ids.length <= 0) {
        this.$message.warning('请选择职务')
        return false
      }
      var _this = this
      this.$confirm({
        title: '删除?',
        content: '是否确定删除该组织类别?',
        onOk() {
          postRemove(ids).then(res => {
            if (res.code === 0) {
              _this.$message.success(`删除成功`)
              _this.handleReload()
            } else {
              _this.$message.error(res.msg)
            }
          })
        },
        onCancel() {}
      })
    },

    onSelectChange(selectedRowKeys, selectedRows) {
      this.ids = []
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
      for (var item of selectedRows) {
        this.ids.push(item.id)
      }
    }
  }
}
</script>
